/*=+--+=#=+--     Unmanned Aerial System Management Software      --+=#=+--+=#*\
|          Copyright (C) 2011 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU General Public License version 2 as
     published by the Free Software Foundation.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>.

            Jack Elston                       Cory Dixon                        
|           elstonj@colorado.edu              dixonc@colorado.edu              |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

/*!
  \file socketThread.h
	\brief header file for internet socket thread receiving data

                    \author Jack Elston
             $Date: 2005/02/12 00:20:45 $
*/
#ifndef __SOCKETTHREAD_H
#define __SOCKETTHREAD_H

#include <unistd.h>
#include <signal.h>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <math.h>
#include <arpa/inet.h>

#include "String.h"
#include "socket.h"
#include "timeLib.h"
#include "type_defs.h"
#include "threadedPipe.h"
#include "shmStruct.h"

  /*<-------Defines--------->*/
    /*! \brief socket buffer size. */

/************************************************************\
|                 communications timing                      |
\************************************************************/
    /*! \brief timeout in seconds between reconnect attempts. */
    #define COMM_RECONNECT_TIMEOUT 2.0
  /*<------End Defines------>*/

/************************************************************\
|                      InernetRx_Socket                      |
\************************************************************/
/*!
  \class SocketThread
  \brief interfaces to a TCP connection and forwards the data
   to the inetRx fifo
  \todo the SocketThread thread hangs if the client has not
   disconnected and SocketThread::close is called.
  \todo the SocketThread thread cannot gracefully handle
   more than one client
 */
class SocketThread : public ThreadedPipe
{
private:
	Socket *sock;
	String hostStr;
	String portStr;
	Socket::SocketType sockType;
	Socket::SocketMode sockMode;

	void initSocket(void);
	bool connectSocket(void);

  int readTCP(fd_set readFDs);
  int readUDP();
  int writeTCP(fd_set writeFDs, int num);
  int writeUDP(int num);

	bool connect_on_init;
	bool trying_to_connect;
	bool waiting_response;
  
	void parse(const Command &cmd, in_addr address);

public:

	SocketThread(String name,
			String port,
			Socket::SocketType type,
			Socket::SocketMode mode,
			String host = "localhost",
			bool connect = true);

  bool init(ShmStruct * shmStructPtr);

	~SocketThread()
  { if( sock != NULL) delete sock; }

  ThreadedPipe::commMode mode();

	void update();
	void close();
};

#endif //define
